package com.example.demo.service.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.constant.Status;
import com.example.demo.entity.goods.GoodsInfo;
import com.example.demo.entity.user.UserInfo;
import com.example.demo.repository.goods.GoodsInfoMapper;
import com.example.demo.service.goods.dto.GoodsInfoDto;
import com.example.demo.service.goods.vo.GoodsInfoVo;
import com.example.demo.service.user.UserService;
import org.jeasy.random.EasyRandom;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsService {
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private UserService userService;

    private static final EasyRandom easyRandom = new EasyRandom();

    public GoodsInfo addGoods(GoodsInfoDto goodsInfoDto){
        GoodsInfo goodsInfo = goodsInfoDto.dtoToEntity();
        goodsInfo.initEntity();

        Long time = System.currentTimeMillis();

        goodsInfo.setCreateTime(time);
        goodsInfo.setUpdateTime(time);

        int insert = goodsInfoMapper.insert(goodsInfo);
        if (insert < 0) {
        //    System.out.println("数据库插入数据错误");
            return null;
        }

        return goodsInfo ;
    }

    public GoodsInfoVo getGoodsById(Long id){
        LambdaQueryWrapper<GoodsInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsInfo::getIsDelete, Status.ENABLE)
                .eq(GoodsInfo::getId,id);

        GoodsInfo goodsInfo = goodsInfoMapper.selectOne(qw);
        if (Objects.isNull(goodsInfo)) {
            return null;
        }

        // 获取创建者和更新者的用户名称
        // todo 使用了lambda语法和集合,这是两个基础知识,需要学习,java8中有
        GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
        BeanUtils.copyProperties(goodsInfo,goodsInfoVo);
        List<UserInfo> users = userService.getUsersByIds(Arrays.asList(goodsInfo.getCreateUser(), goodsInfo.getUpdateUser()));

        Map<Long, UserInfo> userMap = users.stream().collect(
                Collectors.toMap(UserInfo::getId, user -> user, (value1, value2) -> value1)
        );
        goodsInfoVo.setCreateUserName(userMap.get(goodsInfoVo.getCreateUser()).getName());
        goodsInfoVo.setUpdateUserName(userMap.get(goodsInfoVo.getUpdateUser()).getName());
        return goodsInfoVo;

    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGoods(Long id){
        LambdaQueryWrapper<GoodsInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsInfo::getIsDelete, Status.ENABLE)
                .eq(GoodsInfo::getId,id);
        GoodsInfo goodsInfo = goodsInfoMapper.selectOne(qw);
        if (Objects.isNull(goodsInfo)) {
            System.out.println("商品不存在,id"+id);
        }
        goodsInfo.setIsDelete(id);
        int i = goodsInfoMapper.updateById(goodsInfo);
        if (i <= 0) {
            System.out.println("删除失败");
            return Boolean.FALSE;
        }
        return Boolean.TRUE;

    }
    public Page<GoodsInfo> queryUserList(Long pageCurrent,Long pageSize){
        LambdaQueryWrapper<GoodsInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsInfo::getIsDelete, Status.ENABLE);

        Page<GoodsInfo> goodsInfoPage = new Page<>(pageCurrent, pageSize);

        Page<GoodsInfo> goodsInfoPage1 = goodsInfoMapper.selectPage(goodsInfoPage, qw);

        return goodsInfoPage;

    }

    public Page<GoodsInfo> random(Long pageCurrent, Long pageSize) {
        Page<GoodsInfo> goodsInfoPage = new Page<GoodsInfo>();

        int count = 20;
        ArrayList<GoodsInfo> data = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            GoodsInfo mock = easyRandom.nextObject(GoodsInfo.class);
//            GoodsInfo mock = new GoodsInfo();
            mock.setCode(easyRandom.nextLong());
            mock.setName("商品" + 1);
            mock.setPrice(easyRandom.nextDouble() + "");
            mock.setWeight(easyRandom.nextDouble() + "");
            mock.setMaterial("物料" + i);
            mock.setIsDelete(0l);

            mock.setId((long)i);
            data.add(mock);
        }

        Long dataStart = (pageCurrent - 1) * pageSize;
        Long dataEnd = pageCurrent * pageSize >data.size() ? data.size():pageCurrent * pageSize;

        List<GoodsInfo> records = data.subList(Integer.parseInt(dataStart.toString()), Integer.parseInt(dataEnd.toString()));

        goodsInfoPage.setRecords(records);
        goodsInfoPage.setTotal(data.size());
        goodsInfoPage.setSize(pageSize);
        goodsInfoPage.setCurrent(pageCurrent);
        return goodsInfoPage;



    }
}


